package com.fastloan.safe.mgt.auth.controller;

import com.fastloan.safe.core.param.RoleTypeEnum;
import com.fastloan.safe.core.util.ManagerKit;
import com.fastloan.safe.mgt.auth.model.CmuRole;
import com.fastloan.safe.mgt.auth.service.CmuRoleService;
import com.fastloan.safe.mgt.root.service.CmFunService;
import com.kawins.base.baseabs.BaseController;
import com.kawins.result.ResultEnum;
import com.kawins.result.ResultTo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * @author rankai
 *         createTime 2017-10-2017/10/19 15:44
 */
@RestController
@RequestMapping("/api/web/cmuRole")
public class CmuRoleController extends BaseController<CmuRole> {

    @Autowired
    private CmuRoleService roleService;

    @Autowired
    private CmFunService funService;

    /**
     * 角色分页列表
     *
     * @return
     */
    @RequestMapping("/pageList")
    public ResultTo pageList() {
        Map<String, Object> map = new HashMap<>(0);
        map.put("topOrgId", ManagerKit.getTopOrgId());
        return dataPage(map);
    }

    /**
     * 获取增加或者修改的参数
     *
     * @param cmuRoleId
     * @return
     */
    @GetMapping("/param")
    public ResultTo param(@RequestParam(required = false) String cmuRoleId) {
        Map<String, Object> map = new HashMap<>();
        map.put("roleType", RoleTypeEnum.selectByGroup(RoleTypeEnum.Group.GROUP_USER));
        if (StringUtils.isBlank(cmuRoleId)) {
            map.put("defFunTreeVos", funService.selectFunTreeByOrg());
        } else {
            map.put("cmuRole", adaptService.selectByPrimaryKey(cmuRoleId));
            map.put("defFunTreeVos", funService.selectFunTreeByOrgCheck(cmuRoleId));
        }
        return new ResultTo().setData(map);
    }

    /**
     * 增加角色
     *
     * @param cmuRole
     * @param funIds
     * @return
     */
    @PostMapping
    public ResultTo insert(CmuRole cmuRole, String[] funIds) {
        if (funIds == null || funIds.length == 0) {
            return new ResultTo(ResultEnum.PARAM_ERROR, "角色功能不能为空");
        }
        roleService.insertWithFuns(cmuRole, funIds);
        return new ResultTo();
    }

    /**
     * 更新角色
     *
     * @param cmuRole
     * @param funIds
     * @return
     */
    @PutMapping
    public ResultTo update(CmuRole cmuRole, String[] funIds) {
        if (funIds == null || funIds.length == 0) {
            return new ResultTo(ResultEnum.PARAM_ERROR, "角色功能不能为空");
        }
        roleService.updateWithFuns(cmuRole, funIds);
        return new ResultTo();
    }

    /**
     * 删除角色
     *
     * @param cmuRoleId
     * @return
     */
    @DeleteMapping("/{cmuRoleId}")
    public ResultTo delete(@PathVariable String cmuRoleId) {
        return new ResultTo();
    }
}
